<!--  -->
<template>
    <div>
        <van-form @submit="changeFromData">

            <!-- 头 -->
            <div class="font15 mt10 pl10">
                <span class="titletext">{{ nFromdata.title }}</span>
                <div class="graytext mt10 font13">
                    <span>表单编号：</span>
                    <span class="titletext">{{ nFromdata.pmInsId }}</span>
                </div>
            </div>


            <van-tabs v-model="active" color="#1989fa" sticky animated  :lazy-render ="false" line-width="33%" offset-top="50">

                <van-tab title="基础信息">

                    <XuTableOne ref="toMsgs" :fromdata='fromdata' :showHistory='showHistory' :approveList='approveList'
                        :historydata='historydata' :location="params.location" :aiDataResult="aiDataResult"/>
                </van-tab>

                <van-tab title="其他信息">
                    <XuTableTwo ref="tableTwo" :fromdata='fromdata' :mergeAbilityInfoMode="mergeAbilityInfoMode"  :location="params.location" :todotype='todotype'
                        @freshDat='freshData($event)' />
                </van-tab>
            </van-tabs>


            <!-- 跳转意见 -->
            <div class="left_botton">
                <button block type="button" @click="tomsg()" style="color:#60a9ec; display: inline-block;width:100%;border: 0px solid #60a9ec;border-radius: 15px 0px 0px 15px;
            padding:10px 10px 10px 15px;font-size:13px">意见</button>
            </div>


            <!-- 流转下一步 -->
            <div v-if="todotype||toRead" class="footer">
                <van-button block type="info" native-type="submit" :disabled="!canToNext"
                    style="display: inline-block; width: 100%; border: 0px solid #60a9ec">{{ buttonTitle }}</van-button>
            </div>

        </van-form>

    </div>
</template>



<script>
import {
  getFormDetail,
  getEstablishedProjectByPmInsId,
  getWfLinkTracks,
  getWfOptMags, fileParsingRequestAnalysisFlowQuery, getMergeAbilityInfoList,
} from "@/api/dictapi";

import { mapGetters } from "vuex";
import { Notify, Toast } from "vant";
import { openFileUrl } from "@/store/openFile";
import util from "@/assets/js/public";

import XuTableOne from "./xuTableOne.vue";
import XuTableTwo from "./xuTableTwo.vue";



export default {
    data() {
        return {

            fromdata: {},
            nFromdata: {},
            mergeAbilityInfoMode:{},
            showHistory: false,
            historydata: {},
            approveList: [],

            params: this.$route.query,
            todotype: false,
            active: 0,
            toRead: false,
            buttonTitle: '流转下一步',
            canToNext: true,
            aiDataResult:{}
        };
    },

    computed: {
        ...mapGetters(["user"]),
        ...mapGetters(["userinfo"]),

    },

    //生命周期--创建完成
    created() {
        if(this.params.pmInsType==null&&this.params.processType!=null) {
          this.params.pmInsType = this.params.processType
        }
        if (this.params.todoType == "1") {
            this.todotype = true;
        }
        else if (this.params.todoType == "2") {
            this.showHistory = true;
        }else if (this.params.todoType == "3") {
            this.toRead = true;
        }

        if (this.params.location == 'dict.start' || 
            this.params.location == 'dict.directoToAdmin' ||
            this.params.location == 'dict.GiveMasterToGiveManager' ) {
                
            this.canToNext = false;
            this.buttonTitle = '暂不支持该环节,请至PC端办理';
        }

        this.getdetail();
    },

    //生命周期--挂载完成
    mounted() { },

    //方法调用
    methods: {

        freshData(value) {
            this.fromdata = value
        },

        tomsg() {

            this.active = 0;
            setTimeout(() => {
                document.getElementById('opinion').scrollIntoView({ behavior: 'smooth' });
            }, 350);
        },

        openFileUrlfun(value) {
            this.$router.push({
                name: "fileview",
                query: {
                    openFileUrl: openFileUrl(value),
                },
            });
        },

        getdetail() {
            Toast.loading({ message: "加载中", forbidClick: false, });
            getFormDetail(Object.assign(this.params, { userName: this.user.userName })).then((result) => {
                this.fromdetal = {};
                Toast.clear();
                if (!result.data) {
                    Notify(result.message);
                    return;
                }
                if (result.data.data) {
                    this.nFromdata = result.data.data;
                }
            });

            getEstablishedProjectByPmInsId(Object.assign(this.params, { userName: this.user.userName })).then((result) => {
                this.fromdetal = {};
                Toast.clear();
                if (!result.data) {
                    Notify(result.message);
                    return;
                }
                if (result.data.data) {
                    this.fromdata = result.data.data;
                    this.$nextTick().then(() => {
                        // this.$refs.toMsgs.checkMeetingsAndCB()
                        this.$refs.tableTwo.getParentParams();
                    })
                    if (this.fromdata?.mergeAbilityInfoMode){
                      this.mergeAbilityInfoMode=this.fromdata.mergeAbilityInfoMode
                    }else {
                      getMergeAbilityInfoList({userName:this.user.userName,businessId:this.fromdata.businessId})
                          .then((result)=>{
                            Toast.clear();
                            if(!result.data){
                              Notify(result.message);
                              return;
                            }
                            if(result.data.data){
                              this.mergeAbilityInfoMode=result.data.data[0]
                            }
                          })
                    }
                }
            });

            getWfOptMags(Object.assign(this.params), { userName: this.user.userName }).then((result) => {
                Toast.clear();
                if (!result.data) {
                    Notify(result.message);
                    return;
                }
                this.approveList = result.data.data.reverse();
            });

            if (this.showHistory) {
                getWfLinkTracks(Object.assign(this.params), { userName: this.user.userName }).then((result) => {
                    Toast.clear();
                    if (!result.data) {
                        Notify(result.message);
                        return;
                    }
                    this.showHistory = false;
                    if (result.data.data) {
                        //数据过滤，并生成新的数组
                        this.historydata = result.data.data.filter((item) => item.CURRENTSTATE == 10)[0];
                        if (this.historydata) {
                            if (this.historydata.MANAGER) {
                                this.showHistory = true;
                            }
                        }
                    }
                });
            }
            if(this.fromdata.decisionMaterials){
              Toast.loading({ message: "加载中", forbidClick: false, });
              fileParsingRequestAnalysisFlowQuery(Object.assign(this.params, { userName: this.user.userName, fileId: this.fromdata.decisionMaterials[0].id })).then((result) => {
                Toast.clear();
                if (!result.data) {
                  Notify(result.message);
                  return;
                }
                if (result.data.data) {
                  this.aiDataResult = result.data.data;
                }
              });
            }
        },

        /// 
        changeFromData() {

            var formSendData = { "htmlParamMap": {} };
            formSendData.htmlParamMap = this.fromdata;
            formSendData.id = this.nFromdata.id;
            formSendData.pmInsId = this.nFromdata.pmInsId;
            formSendData.processType = this.params.pmInsType
            formSendData.workItemId = this.params.workItemId;

            var params1 = {
                kv: {
                    currentUserCode: util.encrypt(this.params.userName),
                    source: 'MOBILE',
                    location: this.params.location,
                    processInstId: this.params.processInstId,
                    processDefName: this.params.processDefName,
                    processType: this.params.pmInsType,
                },

                body: {
                    location: this.params.location,
                    processInstId: this.params.processInstId,
                    processDefName: this.params.processDefName,
                    processType: this.params.pmInsType,
                }
            };

            var params2 = {
                kv: {
                    "currentUserCode": util.encrypt(this.params.userName),
                    "source": 'MOBILE',
                    "location": this.params.location,
                    "processInstId": this.params.processInstId,
                    'processType': this.params.pmInsType,
                    'receiptCode': this.params.processInstId
                },
            };

            var params3 = {
                kv: {
                    "source": 'MOBILE',
                    "currentUserCode": util.encrypt(this.params.userName),
                    "workItemId": this.params.todoType==3?"":this.params.workItemId,
                    "processInstId": this.params.processInstId,
                    "location": this.params.location,
                    "notificationId":this.params.todoType==3?this.params.wfnid:""
                },
            };
            localStorage.setItem('submitPageParam', JSON.stringify({
                    getDecisionsUrl: 'decisionAndUser$api$getDecisions',
                    getUserUrl: 'decisionAndUser$api$getOrgAndUser',
                    tosubmintUrl: 'applicationFormEnhance$api$startSubmitProcess',

                    getDecisionsParams: params1,
                    getUserParams: params2,
                    tosubmintParams: params3,
                    formData: formSendData,
                    fromMoa:this.params.fromMoa,
                    type:this.params.todoType==3?"toRead":"toDo"
                }));
            this.$router.push({
                name: "srqrsubmit"
            });
        },
    },

    components: { XuTableOne, XuTableTwo }
};


</script>
<style  scoped>
/* @import url();*/
</style>